import numpy as np
import pandas as pd
from pandas import Series,DataFrame

df1 = DataFrame({
    'key' :['b', 'b', 'a', 'c', 'a', 'a', 'b'],
    'data1':range(7)
})
df2 = DataFrame({
    'key' :['a', 'b', 'd'],
    'data2':range(3)
})

print df1
print df2
print pd.merge(df1, df2)
print pd.merge(df1, df2, on='key')

df3 = DataFrame({
    'lkey' :['b', 'b', 'a', 'c', 'a', 'a', 'b'],
    'data1':range(7)
})

df4 = DataFrame({
    'rkey' :['a', 'b', 'd'],
    'data2':range(3)
})

print df3
print df4
print pd.merge(df3, df4, left_on='lkey', right_on='rkey')

print "-----------------------------2-----------------------------"

lefth = DataFrame(
    {'key1':['Ohio','Ohio', 'Ohio', 'Nevada','Nevada'],
    'key2':[2001, 2000, 2002, 2001, 2002],
    'data':np.arange(5.)}
)

righth = DataFrame(np.arange(12).reshape((6,2)),
                   index=[
                       ['Nevada','Nevada','Ohio','Ohio','Ohio','Ohio'],
                       [2001, 2000, 2000, 2000, 2001, 2002]],
                   columns=['event1', 'event2'])
print lefth
print righth
print pd.merge(lefth, righth, left_on=['key1', 'key2'], right_index=True)

print "-------------------------3--------------------------"
s1 = Series([0,1], index=['a', 'b'])
s2 = Series([2,3 ,4], index=['c', 'd','e'])
s3 = Series([5,6], index=['f', 'g'])

print s1, s2, s3
print pd.concat([s1, s2, s3])
print pd.concat([s1, s2], axis=1)
print pd.concat([s1, s2, s3], axis=1)
print "------------------------4--------------------------------"
s4 = pd.concat([s1*5, s3])
print s1, s4
print pd.concat([s1, s4], axis=1)
print pd.concat([s1, s4], axis=1, join='inner')

print "-------------------------5--------------------------------"
a = Series([np.nan, 2.5, np.nan, 3.5, 4.5, np.nan],index=['f', 'e', 'd', 'c', 'b', 'a'])
b = Series(np.arange(len(a), dtype=np.float64),index=['f', 'e', 'd', 'c', 'b', 'a'])
print a,b
b[-1] = np.nan
print np.where(pd.isnull(a), b, a)

